package edu.ielab.localansys.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.apache.oro.text.regex.Perl5Substitution;
import org.apache.oro.text.regex.StringSubstitution;
import org.apache.oro.text.regex.Util;

public class FileUtil {
	public static String readFile(String path) {
		File file = new File(path);
        StringBuffer contents = new StringBuffer();
        BufferedReader reader = null;

        try
        {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            // repeat until all lines is read
            while ((text = reader.readLine()) != null)
            {
                contents.append(text)
                    .append(System.getProperty(
                        "line.separator"));
            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                if (reader != null)
                {
                    reader.close();
                }
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        
        // show file contents here
        System.out.println(contents.toString());
		return contents.toString();
	}
	
	public static void writeFile(String content, String path) {
		File f=new File(path);
	      FileOutputStream fop;
		try {
			fop = new FileOutputStream(f);
			if(f.exists()){
				fop.write(content.getBytes());
				fop.flush();
				fop.close();
				System.out.println("The data has been written");
			}
			
			else
				System.out.println("This file is not exist");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	/**
	 * content = '1122A33' after operate-> '1122B33' 
	 * d1=20
	 */
	public static String substituteFile(String content, String A, String B) {
		String A1 = A.split("=")[0];
		PatternMatcher matcher = new Perl5Matcher();
		PatternCompiler pc = new Perl5Compiler();
		Pattern p = null;
		try {  
            p = pc.compile(A1+"=[0-9]+.?[0-9]*", Perl5Compiler.DEFAULT_MASK);  
        } catch (MalformedPatternException e) {  
            System.out.println("regex compile error: " + e.getStackTrace());  
        }  
		  StringSubstitution sub = new Perl5Substitution(B);
		  content = Util.substitute(matcher, p, sub, content);
		return content;
	}
}
